




package com.lkm.app_ui.help;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.preference.PreferenceManager;
import android.util.Log;

import java.io.Closeable;
import java.io.IOException;

import static android.content.Context.AUDIO_SERVICE;



public final class BeepManager implements OnCompletionListener, OnErrorListener, Closeable {
    private static final String TAG = BeepManager.class.getSimpleName();
    private static final float BEEP_VOLUME =1f;
    private final Activity activity;
    private MediaPlayer mediaPlayer;
    private boolean playBeep;
    private int beepResourceId;

    public BeepManager(Activity activity, int resouceId) {
        this.activity = activity;
        this.mediaPlayer = null;
        this.beepResourceId = resouceId;
        this.updatePrefs();
    }

    public synchronized void updatePrefs() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.activity);
        this.playBeep = shouldBeep(prefs, this.activity);
        if(this.playBeep && this.mediaPlayer == null) {
            this.activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
            this.mediaPlayer = this.buildMediaPlayer(this.activity);
        }

    }

    public synchronized void playBeepSoundAndVibrate() {
        if(this.playBeep && this.mediaPlayer != null) {
            this.mediaPlayer.seekTo(0);
            this.mediaPlayer.start();
        }

    }
    public synchronized void stopBeepSoundAndVibrate() {
        if(this.playBeep && this.mediaPlayer != null) {
            this.mediaPlayer.stop();
        }

    }

    private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
        boolean shouldPlayBeep = true;
        if(shouldPlayBeep) {
            AudioManager audioService = (AudioManager)activity.getSystemService(AUDIO_SERVICE);
            if(audioService.getRingerMode() != 2) {
                shouldPlayBeep = false;
            }
        }

        return shouldPlayBeep;
    }

    private MediaPlayer buildMediaPlayer(Context activity) {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnErrorListener(this);

        try {
            AssetFileDescriptor ioe = activity.getResources().openRawResourceFd(this.beepResourceId);

            try {
                mediaPlayer.setDataSource(ioe.getFileDescriptor(), ioe.getStartOffset(), ioe.getLength());
            } finally {
                ioe.close();
            }

//            mediaPlayer.setVolume(0.1F, 0.1F);
            mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
            mediaPlayer.prepare();
            return mediaPlayer;
        } catch (IOException var8) {
            Log.w(TAG, var8);
            mediaPlayer.release();
            return null;
        }
    }

    public void onCompletion(MediaPlayer mp) {
        mp.seekTo(0);
    }

    public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
        if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
            this.activity.finish();
        } else {
            mp.release();
            this.mediaPlayer = null;
            this.updatePrefs();
        }

        return true;
    }

    public synchronized void close() {
        if(this.mediaPlayer != null) {
            this.mediaPlayer.release();
            this.mediaPlayer = null;
        }

    }
}
